package dk.marten.autodoodle;

import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class BallView extends View {
	Dot dot;
	Paint paint;
	int bredde = 100;
	int hojde = 100;
	
	private Canvas tempCanvas; 
	private Bitmap tBMP; 

    public float targetHue = 1f;
    public float currentHue = 0f;
    private float actualHue = 2f;

	Random myRandom;
	boolean isStarted = false;
	boolean backgroundColorSet = false;
	ArrayList<SmallDot> allSmallDots;

	public SharedPreferences sharedPreferences;
	

	public BallView(Context context) {
		super(context);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.rgb(250, 250, 250));
		myRandom = new Random();
		dot = new Dot();
		allSmallDots = new ArrayList<SmallDot>();
		
		
	}

	int arrayListIndex;
	int arrayListCount;

	@Override
	protected void onDraw(Canvas currentCanvas) {	

        if (targetHue != currentHue || !backgroundColorSet) {
            double dHue = targetHue-currentHue;
            if (Math.abs(dHue) > 180) {
                if (dHue >180) dHue -= 180;
                else dHue += 180;
                dHue = -dHue;
            }

            dHue = Math.max(-1, Math.min(1, dHue/100));
            currentHue += dHue;

            while (currentHue < 0) currentHue += 360;
            while (currentHue > 360) currentHue -= 360;

            if (Math.abs(actualHue-currentHue) > 0.5) {
            	float[] farve = new float[]{currentHue,1,0.55f};	
        		setBackgroundColor(Color.HSVToColor(farve));
                actualHue = currentHue;
            }
            if ((targetHue-currentHue) > -0.1 && (targetHue-currentHue) < 0.1) currentHue = targetHue;

			 SharedPreferences.Editor editor = sharedPreferences.edit();
             editor.putFloat("CurrentHue", currentHue);
             editor.commit();

             backgroundColorSet=true;
             
        }  
            
        if (!isStarted) return;
        
		if(tBMP == null){
			tBMP = Bitmap.createBitmap(bredde, hojde, Bitmap.Config.ARGB_8888);
			tempCanvas = new Canvas(tBMP);
		}

		SmallDot newDot = new SmallDot();
		newDot.x = dot.x;
		newDot.y = dot.y;
		allSmallDots.add(newDot);

		newDot.draw(tempCanvas);
				
		currentCanvas.drawBitmap(tBMP, new Rect(0,0,bredde,hojde),new Rect(0,0,bredde,hojde), null);
		
		dot.update();
		dot.draw(currentCanvas);
		try {
			Thread.sleep(40);
		} catch (InterruptedException e) {
		}

		invalidate();
	}

	@Override
	public void onSizeChanged(int w, int h, int oldW, int oldH) {
		bredde = w;
		hojde = h;
		
		dot.bredde = w - dot.radius;
		dot.hojde = h - dot.radius;

		if (!isStarted) {
			dot.x = w / 2;
			dot.y = h / 2;
		}
		isStarted = true;
	}

	public void clear() {
		tBMP = Bitmap.createBitmap(bredde, hojde, Bitmap.Config.ARGB_8888);
		tempCanvas = new Canvas(tBMP);
	}

	public void sensorUpdate(float x, float y, float z) {
		dot.xAcc = -Math.max(Math.min(x, 20), -20) / 40;
		dot.yAcc = Math.max(Math.min(y, 20), -20) / 40;
	}

	@Override
	public boolean onTouchEvent(MotionEvent e) {
		switch (e.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// finger touches the screen
			dot.mouseX = e.getX();
			dot.mouseY = e.getY();
			dot.isMouseMove = true;
			break;
		case MotionEvent.ACTION_MOVE:
			dot.mouseX = e.getX();
			dot.mouseY = e.getY();
			dot.isMouseMove = true;
			break;
		case MotionEvent.ACTION_UP:
			dot.isMouseMove = false;
			break;
		}

		return true;
	}

	public float[] getSmallDotPos(boolean isX) {
		float[] xys = new float[allSmallDots.size() - 1];

		arrayListCount = allSmallDots.size() - 1;

		for (arrayListIndex = 0; arrayListIndex < arrayListCount; arrayListIndex++) {
			if (isX)
				xys[arrayListIndex] = allSmallDots.get(arrayListIndex).x;
			else
				xys[arrayListIndex] = allSmallDots.get(arrayListIndex).y;
		}
		return xys;
	}

	public float[] getSpeed() {
		return  new float[]{dot.xSpeed,dot.ySpeed};
	}
	
	public float[] getAcc() {
		return  new float[]{dot.xAcc,dot.yAcc};
	}
	
	public float[] getPosition() {
		return  new float[]{dot.x,dot.y};
	}
	
	
	public void setSpeed(float[] speed) {
		dot.xSpeed = speed[0];
		dot.ySpeed = speed[1];
	}
	
	public void setAcc(float[] acc) {
		dot.xAcc = acc[0];
		dot.yAcc = acc[1];
	}
	
	public void setPosition(float[] pos) {
		isStarted = true;
		dot.x = pos[0];
		dot.y = pos[1];
	}
	
	
	public void setSmallDotPos(float[] xPoser, float[] yPoser) {
		allSmallDots = new ArrayList<SmallDot>();		
		for (int i = 0;i<xPoser.length;i++){
			SmallDot newDot = new SmallDot();
			newDot.x = xPoser[i];
			newDot.y = yPoser[i];
			allSmallDots.add(newDot);
		}
	}



}
